//
// Created by ziya on 2021/4/28.
//

#include "../../include/common.h"
#include "../../include/jni/com_ziya_jvm_jdk_classes_Threads.h"
#include "../../share/vm/memory/Universe.h"
#include "../../share/vm/memory/genCollectedHeap.h"
#include "../../share/vm/gc_implementation/shared/VM_GenCollectForAllocation.h"
#include "../../share/vm/runtime/VMThread.h"

void universe_init();
bool universe_post_init();
void init_globals();

/*
 * Class:     com_ziya_jvm_jdk_classes_Threads
 * Method:    createVM
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_ziya_jvm_jdk_classes_Threads_createVM
        (JNIEnv *env, jclass clazz)
{
    init_globals();
}

/*
 * Class:     com_ziya_jvm_jdk_classes_Threads
 * Method:    gc
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_ziya_jvm_jdk_classes_Threads_gc
        (JNIEnv *env, jclass clazz)
{
    VM_GenCollectForAllocation op(16);
    VMThread::execute(&op);
}

/*
 * Class:     com_ziya_jvm_jdk_classes_Threads
 * Method:    fullGc
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_ziya_jvm_jdk_classes_Threads_fullGc
        (JNIEnv *env, jclass clazz)
{

}

//=====
void init_globals() {
    INFO_PRINT("init_globals\n");

    universe_init();
    universe_post_init();
}

void universe_init() {
    INFO_PRINT("\t universe_init\n");

    Universe::initialize_heap();
}

bool universe_post_init() {
    GenCollectedHeap* heap = (GenCollectedHeap *)Universe::heap();

    heap->post_initialize();

    return true;
}